package com.sczmgk.hotline.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;

import com.sczmgk.hotline.R;
import com.sczmgk.hotline.utils.CommonUtils;

/**
 * Created by Administrator on 2016/12/13.
 */

public class MyRadioButton extends RadioButton {
    private Context context;
    public MyRadioButton(Context context) {
        super(context);
        this.context = context;
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MyRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    /**
     * 设置新消息红点
     * @param leftResId  红点资源
     * @param isShowPoint   是否展示红点
     */
    public  void setDrawable(int leftResId, boolean isShowPoint){
        float screenDensity = CommonUtils.getScreenDensity(context);
        float r1 = 25*screenDensity;
        float b1 = 25*screenDensity;
        int r = (int)r1;
        int b = (int)b1;
        Drawable leftImage = context.getResources().getDrawable(leftResId);//将资源转成Drawable
        Drawable rightImage = context.getResources().getDrawable(R.drawable.red_point);//将资源转成Drawable
        leftImage.setBounds(1, 1, r, b);//设置图片的大小
        int pl = -r-(int)(5*screenDensity);
        int pt = -b-(int)(5*screenDensity);
        int pr = -r+(int)(3*screenDensity);
        int pb = -b+(int)(3*screenDensity);
        rightImage.setBounds(pl, pt, pr, pb);

        if (isShowPoint) {
            setCompoundDrawables( null, leftImage,rightImage, null);//设置图片在上面边显示
        }else{
            setCompoundDrawables( null, leftImage,null, null);//设置图片在上面边显示
        }

        setGravity(Gravity.CENTER);

    }

}
